package cn.edu.sjtu.literature.web.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.edu.sjtu.literature.entity.User;
import cn.edu.sjtu.literature.service.LiteratureService;
import cn.edu.sjtu.literature.service.UserService;
import cn.edu.sjtu.literature.service.impl.DefaultServiceFactory;
import cn.edu.sjtu.literature.web.util.Constants;
import cn.edu.sjtu.literature.web.view.DefaultView;
import cn.edu.sjtu.literature.web.view.RedirectView;
import cn.edu.sjtu.literature.web.view.View;

public abstract class AbstractController implements Controller {
	
	protected final Log log = LogFactory.getLog(this.getClass());
	
	protected UserService userService = DefaultServiceFactory.getInstance().getUserService();

	protected LiteratureService literatureService = DefaultServiceFactory.getInstance().getLiteratureService();
	
	protected View getView(String view) {
		return new DefaultView("/WEB-INF/view/" + view);
	}
	
	protected View getRedirectView(HttpServletRequest req, HttpServletResponse resp, String view) throws IOException {
		log.debug("send redirect to: " + req.getContextPath() + "/service/" + view);
		return new RedirectView(req.getContextPath() + "/service/" + view);
	}
	
	protected String getFieldErrorKey(String field) {
		return field + Constants.FIELD_ERROR_KEY;
	}
	
	protected User getSessionUser(HttpServletRequest req) {
		return (User) req.getSession().getAttribute(Constants.SESSION_USER_KEY);
	}
	
}
